
package com.dy.template;

import java.io.IOException;
import java.util.List;

import com.dy.model.Column;
import com.dy.model.Creator;
import com.dy.model.TableInfo;

import cn.dy.util.FileUtil;

/**
 * @Description:
 * @Date：2018年7月9日 @author： DongY
 * @version ：1.0 1.[2018年7月9日]CreatBy：dy
 */

public class Sql implements BaseSting {

	@Override
	public String getStr(final TableInfo tableInfo, final Creator c) {
		final List<Column> columns = tableInfo.getColumns();
		String classString = "";
		try {
			classString = FileUtil.readAsString(c.getTemplatePath());
		} catch (final IOException e) {

			e.printStackTrace();
			System.exit(0);
		}
		String colums = "";
		String sqlMapper = "";
		for (final Column col : columns) {
			col.getJavaName();
			colums += "," + col.getName();
			if (col.getJavaName().equals(tableInfo.getPriKeyColumn())) {
				sqlMapper = "\t\t<id column=\"" + col.getName() + "\" property=\"" + col.getJavaName()
						+ "\" javaType=\"" + col.getJavaType().getSimpleName() + "\" />" + sqlMapper;
			} else {
				sqlMapper += "\r\n\t\t<result column=\"" + col.getName() + "\" property=\"" + col.getJavaName()
						+ "\" javaType=\"" + col.getJavaType().getSimpleName() + "\" />";

			}
		}
		colums = colums.substring(1);
		classString = classString.replace("###cloums###", colums);
		classString = classString.replace("###feildMapper###", sqlMapper);
		return classString;
	}

}
